{% extends 'base.html' %}
{% load static %}

{% block title %}{% if form.instance.pk %}编辑代码{% else %}创建新代码{% endif %} - Creeps Judge{% endblock %}

{% block extra_css %}
<style>
    * {
        box-sizing: border-box;
    }
    
    body {
        background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
        min-height: 100vh;
        position: relative;
    }
    
    body::before {
        content: '';
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: 
            radial-gradient(circle at 20% 80%, rgba(0, 212, 255, 0.1) 0%, transparent 50%),
            radial-gradient(circle at 80% 20%, rgba(100, 150, 255, 0.1) 0%, transparent 50%),
            radial-gradient(circle at 40% 40%, rgba(255, 255, 255, 0.05) 0%, transparent 50%);
        pointer-events: none;
        z-index: -1;
    }
    
    .page-header {
        background: linear-gradient(135deg, rgba(0, 0, 0, 0.8) 0%, rgba(30, 30, 60, 0.9) 100%);
        backdrop-filter: blur(15px);
        color: white;
        padding: 30px 0;
        margin-bottom: 30px;
        text-align: center;
        border-bottom: 2px solid;
        border-image: linear-gradient(135deg, rgba(255, 255, 255, 0.4), rgba(100, 150, 255, 0.6)) 1;
    }
    
    .page-header h1 {
        font-size: 3rem;
        background: linear-gradient(135deg, #00d4ff, #6496ff, #ffffff);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        background-clip: text;
        margin-bottom: 1rem;
        text-shadow: 
            0 0 5px rgba(0, 212, 255, 0.4),
            0 0 10px rgba(0, 212, 255, 0.3),
            0 0 15px rgba(0, 212, 255, 0.2);
        filter: drop-shadow(0 0 8px rgba(0, 212, 255, 0.3));
        font-weight: 600;
    }
    
    .page-header p {
        font-size: 1.2rem;
        color: rgba(255, 255, 255, 0.8);
        margin: 0;
    }
    
    .form-container {
        background: linear-gradient(135deg, rgba(0, 0, 0, 0.8) 0%, rgba(30, 30, 60, 0.9) 100%);
        -webkit-backdrop-filter: blur(15px);
        backdrop-filter: blur(15px);
        border-radius: 20px;
        padding: 3rem;
        box-shadow: 
            0 8px 32px rgba(0, 0, 0, 0.3),
            0 0 20px rgba(100, 150, 255, 0.2),
            inset 0 0 15px rgba(255, 255, 255, 0.05);
        border: 2px solid;
        border-image: linear-gradient(135deg, rgba(255, 255, 255, 0.4), rgba(100, 150, 255, 0.6)) 1;
        margin-bottom: 30px;
        position: relative;
        overflow: hidden;
    }
    
    .form-container::before {
        content: '';
        position: absolute;
        top: 0;
        left: -100%;
        width: 100%;
        height: 100%;
        background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.1), transparent);
        transition: left 0.5s ease;
    }
    
    .form-container:hover::before {
        left: 100%;
    }
    
    .form-group {
        margin-bottom: 2rem;
    }
    
    .form-label {
        font-weight: 600;
        color: rgba(255, 255, 255, 0.9);
        margin-bottom: 12px;
        font-size: 1rem;
        display: block;
    }
    
    .form-control, .form-select {
        background: rgba(255, 255, 255, 0.1);
        border: 2px solid rgba(255, 255, 255, 0.2);
        border-radius: 12px;
        padding: 1rem 1.25rem;
        font-size: 1rem;
        color: white;
        transition: all 0.3s ease;
        backdrop-filter: blur(10px);
        width: 100%;
    }
    
    .form-control::placeholder {
        color: rgba(255, 255, 255, 0.5);
    }
    
    .form-control:focus, .form-select:focus {
        outline: none;
        border-color: rgba(100, 150, 255, 0.8);
        background: rgba(0, 0, 0, 0.4);
        color: rgba(255, 255, 255, 0.95);
        box-shadow: 
            0 0 15px rgba(100, 150, 255, 0.3),
            inset 0 0 10px rgba(255, 255, 255, 0.1);
        transform: translateY(-2px);
    }
    
    /* 语言选择框特殊样式 */
    .form-select {
        background: rgba(0, 0, 0, 0.3);
        color: rgba(255, 255, 255, 0.9);
        cursor: pointer;
    }
    
    .form-select option {
        background: rgba(30, 30, 60, 0.95);
        color: rgba(255, 255, 255, 0.9);
        padding: 8px 12px;
        border: none;
    }
    
    .form-select option:hover, .form-select option:focus {
        background: rgba(100, 150, 255, 0.8);
        color: white;
    }
    
    .form-select:hover {
        background: rgba(45, 55, 85, 0.4);
        border-color: rgba(100, 150, 255, 0.6);
    }
    
    .code-editor-area {
        min-height: 300px;
        font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
        font-size: 14px;
        line-height: 1.6;
        background: rgba(45, 55, 85, 0.3);
        border: 2px solid rgba(255, 255, 255, 0.2);
        border-radius: 12px;
        color: #e8e8e8;
        resize: vertical;
    }
    
    .code-editor-area:focus {
        background: rgba(45, 55, 85, 0.5);
        border-color: rgba(100, 150, 255, 0.8);
        color: rgba(255, 255, 255, 0.95);
        box-shadow: 
            0 0 15px rgba(100, 150, 255, 0.3),
            inset 0 0 10px rgba(255, 255, 255, 0.05);
    }
    
    /* 文本区域样式优化 */
    textarea.form-control {
        background: rgba(45, 55, 85, 0.3);
        color: rgba(255, 255, 255, 0.9);
    }
    
    textarea.form-control:focus {
        background: rgba(45, 55, 85, 0.5);
        color: rgba(255, 255, 255, 0.95);
    }
    
    .btn-gradient {
        background: linear-gradient(135deg, rgba(100, 150, 255, 0.8), rgba(0, 212, 255, 0.8));
        border: 2px solid rgba(255, 255, 255, 0.3);
        color: white;
        padding: 1rem 2rem;
        border-radius: 12px;
        font-weight: 600;
        font-size: 1.1rem;
        transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
        text-decoration: none;
        display: inline-block;
        position: relative;
        overflow: hidden;
        backdrop-filter: blur(10px);
        text-transform: uppercase;
        letter-spacing: 1px;
    }
    
    .btn-gradient::before {
        content: '';
        position: absolute;
        top: 0;
        left: -100%;
        width: 100%;
        height: 100%;
        background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
        transition: left 0.5s ease;
    }
    
    .btn-gradient:hover {
        background: linear-gradient(135deg, rgba(100, 150, 255, 1), rgba(0, 212, 255, 1));
        border-color: rgba(255, 255, 255, 0.8);
        transform: perspective(500px) rotateX(5deg) rotateY(5deg) translateZ(10px) translateY(-3px);
        box-shadow: 
            0 10px 25px rgba(0, 0, 0, 0.3),
            0 0 30px rgba(100, 150, 255, 0.4),
            0 0 15px rgba(255, 255, 255, 0.2);
        color: white;
        text-decoration: none;
    }
    
    .btn-gradient:hover::before {
        left: 100%;
    }
    
    .btn-outline-gradient {
        background: rgba(255, 255, 255, 0.1);
        border: 2px solid rgba(100, 150, 255, 0.6);
        color: #6496ff;
        padding: 1rem 2rem;
        border-radius: 12px;
        font-weight: 600;
        font-size: 1.1rem;
        transition: all 0.3s ease;
        text-decoration: none;
        display: inline-block;
        backdrop-filter: blur(10px);
        position: relative;
        overflow: hidden;
    }
    
    .btn-outline-gradient::before {
        content: '';
        position: absolute;
        top: 0;
        left: -100%;
        width: 100%;
        height: 100%;
        background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.1), transparent);
        transition: left 0.5s ease;
    }
    
    .btn-outline-gradient:hover {
        background: linear-gradient(135deg, rgba(100, 150, 255, 0.8), rgba(0, 212, 255, 0.8));
        color: white;
        border-color: rgba(255, 255, 255, 0.8);
        transform: translateY(-2px);
        box-shadow: 0 8px 20px rgba(100, 150, 255, 0.3);
        text-decoration: none;
    }
    
    .btn-outline-gradient:hover::before {
        left: 100%;
    }
    
    .form-check-container {
        background: rgba(255, 255, 255, 0.05);
        border: 2px solid rgba(100, 150, 255, 0.3);
        border-radius: 15px;
        padding: 20px;
        backdrop-filter: blur(10px);
        transition: all 0.3s ease;
    }
    
    .form-check-container:hover {
        border-color: rgba(100, 150, 255, 0.5);
        background: rgba(255, 255, 255, 0.08);
    }
    
    .form-check-input {
        width: 20px;
        height: 20px;
        margin: 0;
        accent-color: #6496ff;
        cursor: pointer;
        flex-shrink: 0;
        vertical-align: middle;
    }
    
    .form-check-label {
        color: rgba(255, 255, 255, 0.9);
        font-size: 1.1rem;
        font-weight: 500;
        cursor: pointer;
        display: flex;
        align-items: center;
        gap: 8px;
        line-height: 1;
    }
    
    .form-check-label i {
        color: rgba(100, 150, 255, 0.8);
        font-size: 1.2rem;
    }
    
    .form-check-label .text-muted {
        font-size: 0.9rem;
        font-weight: 400;
        margin-left: 8px;
    }
    
    .help-text {
        font-size: 0.875rem;
        color: rgba(255, 255, 255, 0.6);
        margin-top: 8px;
        font-style: italic;
        line-height: 1.4;
    }
    
    .errorlist {
        color: #ff6b7a;
        list-style: none;
        padding: 0;
        margin: 8px 0 0 0;
        font-size: 0.875rem;
    }
    
    .errorlist li {
        background: rgba(220, 53, 69, 0.2);
        border: 1px solid rgba(220, 53, 69, 0.4);
        border-radius: 8px;
        padding: 10px 15px;
        margin-bottom: 8px;
        backdrop-filter: blur(10px);
        box-shadow: 0 4px 15px rgba(220, 53, 69, 0.2);
    }
    
    .breadcrumb-nav {
        background: linear-gradient(135deg, rgba(0, 0, 0, 0.6) 0%, rgba(30, 30, 60, 0.8) 100%);
        backdrop-filter: blur(15px);
        padding: 15px 25px;
        border-radius: 12px;
        margin-bottom: 25px;
        border: 1px solid rgba(255, 255, 255, 0.2);
        box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
    }
    
    .breadcrumb-nav a {
        color: #6496ff;
        text-decoration: none;
        font-weight: 500;
        transition: all 0.3s ease;
        position: relative;
    }
    
    .breadcrumb-nav a::after {
        content: '';
        position: absolute;
        bottom: -2px;
        left: 0;
        width: 0;
        height: 2px;
        background: linear-gradient(135deg, #6496ff, #00d4ff);
        transition: width 0.3s ease;
    }
    
    .breadcrumb-nav a:hover {
        color: #00d4ff;
        text-shadow: 0 0 8px rgba(0, 212, 255, 0.5);
        text-decoration: none;
    }
    
    .breadcrumb-nav a:hover::after {
        width: 100%;
    }
    
    .alert {
        background: rgba(220, 53, 69, 0.2);
        border: 1px solid rgba(220, 53, 69, 0.4);
        border-radius: 12px;
        padding: 1rem;
        margin-bottom: 1.5rem;
        color: #ff6b7a;
        backdrop-filter: blur(10px);
        box-shadow: 0 4px 15px rgba(220, 53, 69, 0.2);
    }
    
    .text-muted {
        color: rgba(255, 255, 255, 0.5) !important;
    }
    
    /* 响应式设计 */
    @media (max-width: 768px) {
        .form-container {
            margin: 1rem;
            padding: 2rem;
        }
        
        .page-header h1 {
            font-size: 2.5rem;
        }
        
        .page-header {
            padding: 40px 0;
        }
        
        .btn-gradient, .btn-outline-gradient {
            padding: 0.875rem 1.5rem;
            font-size: 1rem;
        }
    }
</style>
{% endblock %}

{% block content %}
<div class="page-header">
    <div class="container">
        <h1><i class="fas fa-{% if form.instance.pk %}edit{% else %}plus{% endif %}"></i> {% if form.instance.pk %}编辑代码{% else %}创建新代码{% endif %}</h1>
        <p>{% if form.instance.pk %}修改代码的基本信息{% else %}创建您的第一个代码项目{% endif %}</p>
    </div>
</div>

<div class="container">
    <div class="breadcrumb-nav">
        <a href="{% url 'codes:code_list' %}"><i class="fas fa-arrow-left"></i> 返回代码列表</a>
    </div>
    
    <div class="form-container">

        <form method="post">
            {% csrf_token %}

            {% if form.non_field_errors %}
                <div class="alert alert-danger">
                    {{ form.non_field_errors }}
                </div>
            {% endif %}

            <div class="row">
                <div class="col-md-8">
                    <div class="form-group">
                        <label for="{{ form.title.id_for_label }}" class="form-label">
                            <i class="fas fa-heading"></i> 代码标题
                        </label>
                        <input type="text" class="form-control" id="{{ form.title.id_for_label }}" 
                               name="{{ form.title.name }}" value="{{ form.title.value|default:'' }}"
                               placeholder="为您的代码起一个有意义的名字...">
                        {% if form.title.errors %}
                            <ul class="errorlist">
                                {% for error in form.title.errors %}
                                    <li>{{ error }}</li>
                                {% endfor %}
                            </ul>
                        {% endif %}
                        {% if form.title.help_text %}
                            <div class="help-text">{{ form.title.help_text }}</div>
                        {% endif %}
                    </div>
                </div>
                
                <div class="col-md-4">
                    <div class="form-group">
                        <label for="{{ form.language.id_for_label }}" class="form-label">
                            <i class="fas fa-code"></i> 编程语言
                        </label>
                        <select class="form-select" id="{{ form.language.id_for_label }}" name="{{ form.language.name }}">
                            <option value="python" {% if form.language.value == 'python' %}selected{% endif %}>Python</option>
                            <option value="javascript" {% if form.language.value == 'javascript' %}selected{% endif %}>JavaScript</option>
                            <option value="java" {% if form.language.value == 'java' %}selected{% endif %}>Java</option>
                            <option value="cpp" {% if form.language.value == 'cpp' %}selected{% endif %}>C++</option>
                            <option value="c" {% if form.language.value == 'c' %}selected{% endif %}>C</option>
                        </select>
                        {% if form.language.errors %}
                            <ul class="errorlist">
                                {% for error in form.language.errors %}
                                    <li>{{ error }}</li>
                                {% endfor %}
                            </ul>
                        {% endif %}
                        {% if form.language.help_text %}
                            <div class="help-text">{{ form.language.help_text }}</div>
                        {% endif %}
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label for="{{ form.description.id_for_label }}" class="form-label">
                    <i class="fas fa-align-left"></i> 代码描述 <span class="text-muted">(可选)</span>
                </label>
                <textarea class="form-control" id="{{ form.description.id_for_label }}" 
                          name="{{ form.description.name }}" rows="3"
                          placeholder="简要描述这个代码的功能和用途...">{{ form.description.value|default:'' }}</textarea>
                {% if form.description.errors %}
                    <ul class="errorlist">
                        {% for error in form.description.errors %}
                            <li>{{ error }}</li>
                        {% endfor %}
                    </ul>
                {% endif %}
                {% if form.description.help_text %}
                    <div class="help-text">{{ form.description.help_text }}</div>
                {% endif %}
            </div>

            <div class="form-group">
                <div class="form-check-container">
                    <label class="form-check-label" for="{{ form.is_public.id_for_label }}">
                        <input type="checkbox" class="form-check-input" id="{{ form.is_public.id_for_label }}" 
                               name="{{ form.is_public.name }}" {% if form.is_public.value %}checked{% endif %}>
                        <span style="display: flex; align-items: center; gap: 8px;">
                            <i class="fas fa-globe"></i> 公开接受挑战
                            <span class="text-muted">（允许其他用户向此代码发起挑战）</span>
                        </span>
                    </label>
                    {% if form.is_public.errors %}
                        <ul class="errorlist">
                            {% for error in form.is_public.errors %}
                                <li>{{ error }}</li>
                            {% endfor %}
                        </ul>
                    {% endif %}
                    {% if form.is_public.help_text %}
                        <div class="help-text">{{ form.is_public.help_text }}</div>
                    {% endif %}
                </div>
            </div>

            {% if not form.instance.pk %}
                <div class="form-group">
                    <label for="{{ version_form.content.id_for_label }}" class="form-label">
                        <i class="fas fa-file-code"></i> 代码内容
                    </label>
                    <textarea class="form-control code-editor-area" id="{{ version_form.content.id_for_label }}" 
                              name="{{ version_form.content.name }}" rows="15"
                              placeholder="在这里输入您的代码...">{{ version_form.content.value|default:'' }}</textarea>
                    {% if version_form.content.errors %}
                        <ul class="errorlist">
                            {% for error in version_form.content.errors %}
                                <li>{{ error }}</li>
                            {% endfor %}
                        </ul>
                    {% endif %}
                    {% if version_form.content.help_text %}
                        <div class="help-text">{{ version_form.content.help_text }}</div>
                    {% endif %}
                </div>

                <div class="form-group">
                    <label for="{{ version_form.commit_message.id_for_label }}" class="form-label">
                        <i class="fas fa-comment"></i> 版本说明 <span class="text-muted">(可选)</span>
                    </label>
                    <input type="text" class="form-control" id="{{ version_form.commit_message.id_for_label }}" 
                           name="{{ version_form.commit_message.name }}" value="{{ version_form.commit_message.value|default:'' }}"
                           placeholder="简要说明这个版本的特点或变更...">
                    {% if version_form.commit_message.errors %}
                        <ul class="errorlist">
                            {% for error in version_form.commit_message.errors %}
                                <li>{{ error }}</li>
                            {% endfor %}
                        </ul>
                    {% endif %}
                    {% if version_form.commit_message.help_text %}
                        <div class="help-text">{{ version_form.commit_message.help_text }}</div>
                    {% endif %}
                </div>
            {% endif %}

            <div class="d-flex justify-content-between align-items-center mt-4">
                <a href="{% url 'codes:code_list' %}" class="btn-outline-gradient">
                    <i class="fas fa-times"></i> 取消
                </a>
                <button type="submit" class="btn-gradient">
                    <i class="fas fa-{% if form.instance.pk %}save{% else %}plus{% endif %}"></i> 
                    {% if form.instance.pk %}保存修改{% else %}创建代码{% endif %}
                </button>
            </div>
        </form>
    </div>
</div>
{% endblock %}